#!/bin/sh
<#if app.appDescription??>
# ${app.appDescription}
</#if>
###
# chkconfig: ${configuration.initRunLevels} ${configuration.initStartPriority} ${configuration.initStopPriority}
<#if app.appDescription??>
# description: ${app.appDescription}
</#if>
###

user=${configuration.initUser}

# discover BASEDIR
BASEDIR=`dirname "$0"`/..
BASEDIR=`(cd "$BASEDIR"; pwd)`
ls -l "$0" | grep -e '->' > /dev/null 2>&1
if [ $? = 0 ]; then
  #this is softlink
  _PWD=`pwd`
  _EXEDIR=`dirname "$0"`
  cd "$_EXEDIR"
  _BASENAME=`basename "$0"`
  _REALFILE=`ls -l "$_BASENAME" | sed 's/.*->\ //g'`
   BASEDIR=`dirname "$_REALFILE"`/..
   BASEDIR=`(cd "$BASEDIR"; pwd)`
   cd "$_PWD"
fi

cmd="$BASEDIR/bin/${app.appName}"

case "$1" in
  start)
        if [ "$USER" = "$user" ]; then
            $cmd $1
        else
            /bin/su - $user -c "$cmd $1"
        fi
        ;;
  stop)
        if [ "$USER" = "$user" ]; then
            $cmd $1
        else
            /bin/su - $user -c "$cmd $1"
        fi
        ;;
  status)
        if [ "$USER" = "$user" ]; then
            $cmd $1
        else
            /bin/su - $user -c "$cmd $1"
        fi
        ;;
  restart)
        if [ "$USER" = "$user" ]; then
            $cmd $1
        else
            /bin/su - $user -c "$cmd $1"
        fi
        ;;
  *)
        echo "Usage: $0 {start|stop|status|restart}"
        exit 1
esac

exit $?
